﻿@using Aspire.Dashboard.Components.Controls.Grid
@using Aspire.Dashboard.Model
@using Aspire.Dashboard.Resources
@using Aspire.Dashboard.Utils
@using Humanizer
@using Microsoft.Extensions.Diagnostics.HealthChecks

@inject IStringLocalizer<ControlsStrings> ControlStringsLoc
@inject IStringLocalizer<Resources> Loc

<div class="resource-details-layout">

    <FluentToolbar Orientation="Orientation.Horizontal">
        <FluentAnchor Appearance="Appearance.Lightweight" Href="@DashboardUrls.ConsoleLogsUrl(Resource?.Name)" slot="end">@Loc[nameof(Resources.ResourceDetailsViewConsoleLogs)]</FluentAnchor>

        @if (ShowSpecOnlyToggle)
        {
            <FluentIconSwitch Appearance="Appearance.Lightweight"
                              Disabled="IsSpecOnlyToggleDisabled"
                              CheckedTitle="@ControlStringsLoc[nameof(ControlsStrings.EnvironmentVariablesFilterToggleShowSpecOnly)]"
                              UncheckedTitle="@ControlStringsLoc[nameof(ControlsStrings.EnvironmentVariablesFilterToggleShowAll)]"
                              OnToggle="@(() => _showAll = !_showAll)"
                              CheckedIcon="@(new Icons.Regular.Size16.DocumentHeader())"
                              UncheckedIcon="@(new Icons.Regular.Size16.DocumentOnePage())"
                              slot="end"/>
        }

        <FluentIconSwitch @bind-Value="@IsMaskAllChecked"
                          Appearance="Appearance.Lightweight"
                          CheckedTitle="@ControlStringsLoc[nameof(ControlsStrings.EnvironmentVariablesShowVariableValues)]"
                          UncheckedTitle="@ControlStringsLoc[nameof(ControlsStrings.EnvironmentVariablesHideVariableValues)]"
                          OnToggle="@OnMaskAllCheckedChanged"
                          CheckedIcon="@(new Icons.Regular.Size16.Eye())"
                          UncheckedIcon="@(new Icons.Regular.Size16.EyeOff())"
                          class="mask-all-switch"
                          slot="end" />

        <FluentSearch Placeholder="@ControlStringsLoc[nameof(ControlsStrings.FilterPlaceholder)]"
                      Immediate="true"
                      Autofocus="true"
                      @bind-Value="_filter"
                      slot="end" />
    </FluentToolbar>
    <div class="property-grid-container">
        <FluentAccordion>
            <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceDetailsResourceHeader)]" Expanded="true">
                <div slot="end">
                    <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                        @FilteredResourceProperties.Count()
                    </FluentBadge>
                </div>
                <PropertyGrid
                    TItem="ResourcePropertyViewModel"
                    Items="@FilteredResourceProperties"
                    IsValueSortable="false"
                    IsValueMaskedChanged="@OnValueMaskedChanged"
                    HighlightText="@_filter"
                    GridTemplateColumns="1fr 1.5fr" />
            </FluentAccordionItem>
            <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceDetailsEndpointsHeader)]" Expanded="@_isEndpointsExpanded">
                <div slot="end">
                    <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                        @FilteredEndpoints.Count()
                    </FluentBadge>
                </div>
                <PropertyGrid
                    TItem="DisplayedEndpoint"
                    Items="@FilteredEndpoints"
                    ContentAfterValue="(vm) => GetContentAfterValue(vm)"
                    ValueSort="_endpointValueSort"
                    IsValueMaskedChanged="@OnValueMaskedChanged"
                    HighlightText="@_filter"
                    GridTemplateColumns="1fr 1.5fr" />
            </FluentAccordionItem>
            @if (Resource.IsContainer())
            {
                <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceDetailsVolumesHeader)]" Expanded="@_isVolumesExpanded">
                    <div slot="end">
                        <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                            @FilteredVolumes.Count()
                        </FluentBadge>
                    </div>
                    <PropertyGrid
                        TItem="VolumeViewModel"
                        Items="@FilteredVolumes"
                        IsNameSortable="false"
                        IsValueSortable="false"
                        HighlightText="@_filter"
                        GridTemplateColumns="1fr 1.5fr">
                        <ExtraValueContent>
                            <div>
                                @if (context.IsReadOnly)
                                {
                                    <FluentIcon Value="@(new Icons.Regular.Size16.LockClosed())" Style="vertical-align: text-bottom" />
                                }
                                @context.MountType
                            </div>
                        </ExtraValueContent>
                    </PropertyGrid>
                </FluentAccordionItem>
            }
            <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceHealthChecksHeader)]" Expanded="@_isHealthChecksExpanded">
                <div slot="end">
                    <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                        @FilteredHealthReports.Count()
                    </FluentBadge>
                </div>

                <FluentDataGrid ResizeLabel="@AspireFluentDataGridHeaderCell.GetResizeLabel(ControlStringsLoc)"
                                ResizeType="DataGridResizeType.Discrete"
                                Items="@FilteredHealthReports"
                                ItemKey="r => r.Name"
                                ResizableColumns="true"
                                Style="width:100%"
                                GenerateHeader="GenerateHeaderOption.Sticky"
                                GridTemplateColumns="1fr 1fr 1.5fr"
                                ShowHover="true">
                    <AspireTemplateColumn Title="@ControlStringsLoc[nameof(ControlsStrings.NameColumnHeader)]" Class="nameColumn">
                        <GridValue ValueDescription="@ControlStringsLoc[nameof(ControlsStrings.NameColumnHeader)]"
                                   Value="@context.Name"
                                   EnableHighlighting="@(!string.IsNullOrEmpty(_filter))"
                                   HighlightText="@_filter" />
                    </AspireTemplateColumn>
                    <AspireTemplateColumn Title="@ControlStringsLoc[nameof(ControlsStrings.StateColumnHeader)]" Class="stateColumn">
                        <GridValue ValueDescription="@ControlStringsLoc[nameof(ControlsStrings.StateColumnHeader)]"
                                   Value="@(context.HealthStatus?.Humanize() ?? Loc[nameof(Resources.WaitingHealthDataStatusMessage)])"
                                   EnableHighlighting="@(!string.IsNullOrEmpty(_filter))"
                                   HighlightText="@_filter"
                                   TextVisualizerTitle="@context.Name">
                            <ContentBeforeValue>
                                @if (context.HealthStatus is null)
                                {
                                    <FluentProgressRing Width="16px" Style="display: inline-block;" Class="severity-icon" />
                                }
                                else
                                {
                                    // Browse the icon library at: https://aka.ms/fluentui-system-icons
                                    (Icon? icon, Color color) = context.HealthStatus switch
                                    {
                                        HealthStatus.Healthy => ((Icon)new Icons.Filled.Size16.Heart(), Color.Success),
                                        HealthStatus.Degraded => (new Icons.Filled.Size16.HeartBroken(), Color.Warning),
                                        HealthStatus.Unhealthy => (new Icons.Filled.Size16.HeartBroken(), Color.Error),
                                        _ => (new Icons.Regular.Size16.CircleHint(), Color.Info)
                                    };

                                    <FluentIcon Value="@icon" Color="@color" Class="severity-icon"/>
                                }
                            </ContentBeforeValue>
                        </GridValue>
                    </AspireTemplateColumn>
                    <AspireTemplateColumn Title="@ControlStringsLoc[nameof(ControlsStrings.DetailsColumnHeader)]" Class="detailsColumn">
                        <GridValue ValueDescription="@ControlStringsLoc[nameof(ControlsStrings.DetailsColumnHeader)]"
                                   Value="@(context.HealthStatus is null ? Loc[nameof(Resources.WaitingForHealthDataMessage)] : context.DisplayedDescription)"
                                   EnableHighlighting="@(!string.IsNullOrEmpty(_filter))"
                                   HighlightText="@_filter"
                                   TextVisualizerTitle="@context.Name">
                            <ContentInButtonArea>
                                <ExceptionDetails ExceptionText="@context.ExceptionText" />
                            </ContentInButtonArea>
                        </GridValue>
                    </AspireTemplateColumn>
                </FluentDataGrid>
            </FluentAccordionItem>
            <FluentAccordionItem Heading="@ControlStringsLoc[nameof(ControlsStrings.ResourceDetailsEnvironmentVariablesHeader)]" Expanded="@_isEnvironmentVariablesExpanded">
                <div slot="end">
                    <FluentBadge Appearance="Appearance.Neutral" Circular="true">
                        @FilteredEnvironmentVariables.Count()
                    </FluentBadge>
                </div>
                <PropertyGrid TItem="EnvironmentVariableViewModel"
                              Items="@FilteredEnvironmentVariables"
                              IsValueMaskedChanged="@OnValueMaskedChanged"
                              HighlightText="@_filter"
                              GridTemplateColumns="1fr 1.5fr"
                              Class="env-var-properties" />
            </FluentAccordionItem>
        </FluentAccordion>
    </div>
</div>

@code {
    private static RenderFragment GetContentAfterValue(DisplayedEndpoint vm)
    {
        if (vm.Url is null)
        {
            return @<span>@vm.Text</span>;
        }
        else if (string.Equals(vm.Url, vm.Text, StringComparison.Ordinal))
        {
            return @<a href="@vm.Url" target="_blank">@vm.Url</a>;
        }
        else
        {
            return @<a href="@vm.Url" title="@vm.Url" target="_blank">@vm.Text</a>;
        }
    }
}
